//
//  EditBatteryInfoView.swift
//  ezyform
//
//  Created by Moennig Q on 14/9/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
import RxSwift
import RxCocoa
class EditBatteryInfoView: GreenNaviBarView {
    private lazy var disposeBag = DisposeBag()
    let contentView = EditBatteryCommonView()
    var saveBtn = OperationButton(R.string.localizable.save(),enabled: true)
    lazy var addBtn: UIButton = {
        let view = UIButton()
        view.setImage(UIImage(named: "PVDDetail_systeminfo_add"), for: .normal)
        return view
    }()
    override func setUpView(){
        self.backgroundColor = .white
        //MARK: - addbtn
        self.addSubview(addBtn)
        addBtn.snp.makeConstraints { make in
            make.right.equalTo(-8)
            make.height.width.equalTo(40)
            make.centerY.equalTo(backToPreBtn)
        }
        let backView = UIView()
        backView.drawCorner(byRoundingCorners: [.topLeft,.topRight])
        self.addSubview(backView)
        backView.snp.makeConstraints { make in
            make.top.equalTo(topBackView.snp.bottom).offset(-20)
            make.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        backView.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.top.equalTo(Constant.SectionTopMargin)
            make.left.right.bottom.equalToSuperview()
        }
        self.addSubview(saveBtn)
        saveBtn.snp.makeConstraints { make in
            make.bottom.equalTo(OperationButton.bottomMargin)
            make.left.equalTo(Constant.LeftRightMargin)
            make.right.equalTo(-Constant.LeftRightMargin)
            make.height.equalTo(OperationButton.height)
            make.top.equalTo(backView.snp.bottom)
        }
        //订阅isBattery的变化，控制add 按钮的显示
        self.contentView.isBattery.asObservable().subscribe(onNext: {[weak self] (onFlag) in
            self?.addBtn.isHidden = !onFlag
        }).disposed(by: disposeBag)
    }
    func setData(_ list: [InstallationDetailSystemDetailForBatteryModel], isBattery: Bool){
        self.contentView.loadData(list, isBattery: isBattery)
        self.checkEverythingValid()
    }
    //MARK: - 必填项数据绑定控制saveBtn 是否可用
    private func checkEverythingValid(){
        //4.battery
        let isBatteryValid = self.contentView.isBattery.map({$0}).share(replay: 1)
        let batteriesValid = self.contentView.rx.observe(Array<InstallationDetailSystemDetailForBatteryModel>.self, "list").map({ list in
            var validFlag: Bool = true
            if let list = list {
                list.forEach { batteryModel in
                    if batteryModel.battery_model == "" || batteryModel.number_batteries == 0{
                        validFlag = false
                    }
                }
            }
            return validFlag
        }).share(replay: 1)
        //整个battery 信息是否有效
        let everythingValid = Observable.combineLatest(
            isBatteryValid,
            batteriesValid
          ) { !$0 || ($0 && $1)}
          .share(replay: 1)
        
        //
        everythingValid.subscribe { [weak self] (valid) in
            guard let strongSelf = self else {return}
            if valid {
                strongSelf.saveBtn.enableBtn()
            }else{
                strongSelf.saveBtn.disableBtn()
            }
        }.disposed(by: self.disposeBag)
    }
}
